<?php
/* 
 * name        : Internal » JRank Search
 * description : Search using JRank
 * context     : page
 * instructions: Requires key parameter set.
 * 
 */

global $smarty;

$default_config = array(
	'key'        => '',
	'assign'     => false,
	'no_results' => 'There were no results for your search.  Please try another.'
);

$config = array_merge($default_config, parse_config($config));

$start = isset($_REQUEST['start']) and is_numeric($_REQUEST['start']) ? $_REQUEST['start'] : 0;

$result = json_decode(file_get_contents("http://www.jrank.org/api/search/v2.json?key={$config['key']}&start={$start}&q=" . urlencode($_REQUEST['q'])));

if ($config['assign']) {
	$smarty->assign($config['assign'], $result);
	return;
}


?>

<div id="jrank-results">

	<div class="metadata"> 
		<span class="time"><?=$result->meta->time?> seconds</span>
		<span class="count"><?if($result->meta->total>0):?><?=$result->meta->start+1?> &ndash; <?=$result->meta->next_page_start?$result->meta->next_page_start:$result->meta->total?> of <?=$result->meta->total?><?else:?>0<?endif;?> results</span>
		<span class="words">for <strong><?=$result->meta->query?></strong></span>
	</div>
	
	<? if ($result->meta->total == 0): ?>
	<p><?=$config['no_results']?></p>
	<? endif; ?>
	
	<? foreach ($result->entries as $entry): ?>
	<dl> 
		<dt><a href="<?=$entry->url?>"><?=$entry->title?></a></dt>
		<dd class="description"><?=$entry->content?></dd>
		<dd class="url"><?=$entry->url?></dd>
	</dl>
	<? endforeach; ?>
	
	<div class="pagination"> 
		<? if ($result->meta->current_page > 0): ?>
		<a href="?s=<?=urlencode($result->meta->query)?>&amp;start=<?=$result->meta->prev_page_start?>">&laquo; prev</a>
		<? else: ?>
		<span class="disabled">&laquo; prev</span> 
		<? endif; ?>
		
		<?=$result->meta->current_page+1?> of <?=$reult->meta->total_pages?$reult->meta->total_pages+1:1?>
		
		<? if ($result->meta->current_page < $result->meta->total_pages-1): ?>
		<a href="?s=<?=urlencode($result->meta->query)?>&amp;start=<?=$result->meta->next_page_start?>">&laquo; prev</a>
		<? else: ?>
		<span class="disabled">next &raquo;</span>
		<? endif; ?>
	</div>
	
</div>
